using System.Collections.Generic;
namespace Battle.TimelineLog
{
    /// <summary>
    /// 队伍
    /// </summary>
    public class Team<T> : FightId where T : Fighter
    {
        /// <summary>
        /// 队伍的敌我标识，1为self，2为enemy
        /// </summary>
        public int Side;
        /// <summary>
        /// 生存中的战斗参与者
        /// </summary>
        /// <value></value>
        List<T> AliveFighters { get; }
        /// <summary>
        /// 死亡了的战斗参与者（考虑到可能会有战斗完全复活类似的技能）
        /// </summary>
        /// <value></value>
        List<T> DeadFighters { get; }
        Dictionary<int, T> AllFighterDic { get; }
        public Team(int side)
        {
            Side = side;

            AllFighterDic = new Dictionary<int, T>();
            AliveFighters = new List<T>();
            DeadFighters = new List<T>();
        }

        public virtual void Add(T fighter)
        {
            AllFighterDic.TryAdd(fighter.Id, fighter);
            if (fighter.Hp.Value > 0)
                AliveFighters.Add(fighter);
            else
                DeadFighters.Add(fighter);
        }

        public virtual void Dead(T fighter)
        {
            AliveFighters.Remove(fighter);
            DeadFighters.Add(fighter);
        }

        /// <summary>
        /// 获取队员
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public T GetMember(int id)
        {
            T fighter = null;
            AllFighterDic.TryGetValue(id, out fighter);
            return fighter;
        }

        public T GetAliveMember(int idx = 0)
        {
            if (AliveFighters.Count > 0)
            {
                return AliveFighters[idx];
            }
            return null;
        }

        public virtual bool IsLose()
        {
            // 可以自定义队伍失败的条件，默认是全部hp<=0
            return AliveFighters.Count == 0;
        }

    }
}